package com.atwisdom.star.service.status;

import cn.hutool.core.util.StrUtil;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.atwisdom.star.service.pojo.StatusNode;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StatusMachine
 * @ Description:
 * @author:王红艳
 * @date:
 */
@Data
public abstract class StatusMachine {
    List<StatusNode> statusMap = new ArrayList<>();
    List<StatusNode> runStatusMap = new ArrayList<>();
    List<StatusNode> approveStatusMap = new ArrayList<>();
    Long id;
    String status = "";
    String runStatus = "";
    String approveStatus = "";
    String updateStatus = "";
    String updateKey = "";

    final String statusKey = "status";
    final String runStatusKey = "runStatus";
    final String approveStatusKey = "approveStatus";

    /**
     * 实例id
     * @param id 实例id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * 状态赋值
     */
    public void setStatus() {
    }

    /**
     * 状态赋值
     * @param status 状态
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * 运行状态赋值
     * @param runStatus 运行状态
     */
    public void setRunStatus(String runStatus) {
        this.runStatus = runStatus;
    }

    /**
     * 审批赋值
     * @param approveStatus 审批状态
     */
    public void setApproveStatus(String approveStatus) {
        this.approveStatus = approveStatus;
    }

    /**
     * 更新状态赋值
     * @param updateKey 更新状态Key值
     * @param updateStatus 更新状态
     */
    public void setUpdateStatus(String updateKey,String updateStatus){
        this.updateKey = updateKey;
        this.updateStatus = updateStatus;
    }

    /**
     * 状态map复制
     * @param status 状态
     * @param nextStatus 下一状态
     * @return
     */
    public StatusMachine setStatusMap(String status,String nextStatus){
        statusMap.add(new StatusNode(status,nextStatus));
        return this;
    }

    /**
     * 运行状态map复制
     * @param status 状态
     * @param nextStatus 下一状态
     * @return
     */
    public StatusMachine setRunStatusMap(String status,String nextStatus){
        runStatusMap.add(new StatusNode(status,nextStatus));
        return this;
    }

    /**
     * 运行状态map复制
     * @param status 状态
     * @param nextStatus 下一状态
     * @return
     */
    public StatusMachine setApproveStatusMap(String status,String nextStatus){
        approveStatusMap.add(new StatusNode(status,nextStatus));
        return this;
    }

    /**
     * 更新状态
     */
    public void updateStatus(){

    }

    /**
     * 校验并更新状态
     */
    public void next(boolean flag){
        if(isNextStatus()&&flag){
            updateStatus();
        }else{
            throw new BusinessException("状态不对应");
        }
    }

    /**
     * 校验并更新状态
     */
    public void back(boolean flag){
        if(isBackStatus()&&flag){
            updateStatus();
        }else{
            throw new BusinessException("状态不对应");
        }
    }

    /**
     * 校验并更新状态
     */
    public void next(){
        next(true);
    }

    /**
     * 校验并更新状态
     */
    public void back(){
        back(true);
    }

    /**
     * 获取要校验的statusMap
     * @return
     */
    public List<StatusNode> getCheckStatusMap(){
        return statusKey.equals(updateKey)?statusMap:runStatusKey.equals(updateKey)?runStatusMap:approveStatusMap;
    }

    /**
     * 获取要校验的status
     * @return
     */
    public String getCheckStatus(){
        return statusKey.equals(updateKey)?status:runStatusKey.equals(updateKey)?runStatus:approveStatus;
    }

    /**
     * 校验下一状态
     * @return 是否正确
     */
    protected boolean isNextStatus(){
        List<StatusNode> checkStatusMap = getCheckStatusMap();
        String status = getCheckStatus();
        boolean flag = false;
        for(StatusNode statusNode:checkStatusMap){
            if((StrUtil.isEmpty(status)|| status.equals(statusNode.getStart()))&&updateStatus.equals(statusNode.getEnd())){
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 校验上一状态
     * @return 是否正确
     */
    protected boolean isBackStatus(){
        List<StatusNode> checkStatusMap = getCheckStatusMap();
        String status = getCheckStatus();
        boolean flag = false;
        for(StatusNode statusNode:checkStatusMap){
            if(updateStatus.equals(statusNode.getStart())&&status.equals(statusNode.getEnd())){
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 校验状态值
     * @param checkStatus
     * @return
     */
    public boolean checkStatus(String checkStatus){
        return checkStatus.equals(this.getStatus());
    }

    /**
     * 校验运行状态值
     * @param checkStatus
     * @return
     */
    public boolean checkRunStatus(String checkStatus){
        return checkStatus.equals(this.getStatus());
    }

    /**
     * 校验审批状态值
     * @param checkStatus
     * @return
     */
    public boolean checkApproveStatus(String checkStatus){
        return checkStatus.equals(this.getStatus());
    }

}
